package com.sgzn.sgpay.service.impl.bank;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsBankCardBinRequest;
import com.sgzn.sgpay.model.response.SgsBankCardBinResponse;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.persistence.dao.TBankCardBinMapper;
import com.sgzn.sgpay.persistence.domain.TBankCardBin;
import com.sgzn.sgpay.persistence.domain.TBankCardBinExample;
import com.sgzn.sgpay.service.bank.SgsBankCardQueryService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by linliangfang 13580393408
 * DATETIME:2018/6/6 22:12
 * Description:
 */
@Slf4j
@Service
public class SgsBankCardQueryServiceImpl implements SgsBankCardQueryService {

    @Autowired
    private TBankCardBinMapper tBankCardBinMapper;

    /**
     * 根据银行卡号查询银行卡信息
     * @param request 请求参数
     * @return
     */
    @Override
    public  SgsResultResponse<TBankCardBin> queryBankCardByCardNum(SgsBankCardBinRequest request){
        TBankCardBinExample tBankCardBinExample = new TBankCardBinExample();
        TBankCardBinExample.Criteria criteria = tBankCardBinExample.createCriteria();
        criteria.andAccountLengthEqualTo( Integer.toString(StringUtils.length( request.getCardNo())));
        criteria.andAccountPrefixLike(StringUtils.substring(request.getCardNo(),0,6 ));
        log.info("查询条件:{} ",tBankCardBinExample.toString());
        List<TBankCardBin> tBankCardBinList =  tBankCardBinMapper.selectByExample( tBankCardBinExample );
        log.info("查询结果:{} ",tBankCardBinList);
        if (tBankCardBinList == null ){
            log.error("没找到卡Bin,不支持该银行卡");
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_MCH_Bank_NOT_SUPPORT.getResponseCode(),
                    SgsResponseCode.C_MCH_Bank_NOT_SUPPORT.getResponseDesc());
        }

        return SgsResponseUtil.INSTANCE.success(tBankCardBinList.get(0));
    }


    public static void main(String[] args){
        String testStr="6227003320320012042";
        System.out.println("substri(1,6)"+ StringUtils.substring(testStr,0,6));
    }
}
